import service from '../utils/axios'
import type { ApiResponse } from '../utils/axios'

// 题目数据接口
export interface QuestionInfo {
  id: number
  type: string // single, multiple, judge, fill, essay
  content: string // 题干
  options?: Record<string, string> // 选项（JSON格式）
  answer: string // 正确答案
  analysis?: string // 题目解析
  difficulty?: string // easy, medium, hard
  score: number // 基准分数
  courseId: number // 所属课程ID
  createBy?: string
  createTime?: string
  updateBy?: string
  updateTime?: string
}

// 分页查询参数接口
interface QuestionQueryParams {
  type?: string
  courseId?: number
  difficulty?: string
  content?: string
  page?: number
  size?: number
}

// 分页查询响应接口
interface PageResult<T> {
  data: T[]
  total: number
  page: number
  size: number
}

// 创建/更新题目请求接口
interface QuestionRequest {
  type: string
  content: string
  options?: Record<string, string>
  answer: string
  analysis?: string
  difficulty?: string
  score: number
  courseId: number
}

/**
 * 获取题目列表（分页）
 */
export function getQuestionList(data: QuestionQueryParams): Promise<ApiResponse<PageResult<QuestionInfo>>> {
  return service({
    url: '/api/questions/query',
    method: 'post',
    data
  })
}

/**
 * 根据课程ID获取题目列表
 */
export function getQuestionsByCourse(courseId: number): Promise<ApiResponse<QuestionInfo[]>> {
  return service({
    url: `/api/questions/course/${courseId}`,
    method: 'get'
  })
}

/**
 * 获取题目详情
 */
export function getQuestionById(id: number): Promise<ApiResponse<QuestionInfo>> {
  return service({
    url: `/api/questions/${id}`,
    method: 'get'
  })
}

/**
 * 创建题目
 */
export function createQuestion(data: QuestionRequest): Promise<ApiResponse<{ id: number }>> {
  return service({
    url: '/api/questions',
    method: 'post',
    data
  })
}

/**
 * 更新题目
 */
export function updateQuestion(id: number, data: QuestionRequest): Promise<ApiResponse<void>> {
  return service({
    url: `/api/questions/${id}`,
    method: 'put',
    data
  })
}

/**
 * 删除题目
 */
export function deleteQuestion(id: number): Promise<ApiResponse<void>> {
  return service({
    url: `/api/questions/${id}`,
    method: 'delete'
  })
}



